<?php


namespace app\admin\controller;


use app\common\controller\admin\Base;

class Sourcematerialconfig extends Base
{
    protected $noNeedLogin = [];

    protected $model;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\SourceMaterialConfig();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        if ($this->request->isPost()) {
            $page = $this->request->post('page', 1);
            $size = $this->request->post('page_size', 10);
            $title = $this->request->post('title', '');
            $name = $this->request->post('name', '');
            $content = $this->request->post('content', '');
            $primaryClassification = $this->request->post('primary_classification', '');
            $secondaryClassification = $this->request->post('secondary_classification', '');
            $sort = $this->request->post('sort', 'id');
            $order = $this->request->post('order', 'desc');
            $adminId = $this->request->post('admin_id', '');

            if ($adminId != '') {
                $where['admin_id'] = $adminId;
            }
            if ($name != '') {
                $where['name'] = ['like', '%' . $name . '%'];
            }
            if ($title != '') {
                $where['title'] = ['like', '%' . $title . '%'];
            }
            if ($content != '') {
                $where['content'] = ['like', '%' . $content . '%'];
            }
            if ($primaryClassification != '') {
                $where['primary_classification'] = $primaryClassification;
            }
            if ($secondaryClassification != '') {
                $where['secondary_classification'] = $secondaryClassification;
            }
            $where['is_del'] = 0;
            $list = $this->model
                ->field('*')
                ->where($where)
                ->page($page, $size)
                ->order($sort, $order)
                ->select();
            $total = $this->model->where($where)->count('id');
            $totalPage = ceil($total / $size);
            $data = [
                'total' => $total,
                'list' => $list,
                'totalPage' => $totalPage
            ];
            $this->success('列表', '', $data);
        }
    }

    /**
     * 单独获取所有素材列表
     */
    public function getSourceMaterialConfigList()
    {
        $sort = $this->request->post('sort', 'id');
        $order = $this->request->post('order', 'desc');
        $where = [
            'is_del' => 0,
            'status' => 1,
            'partner_id' => 0 //官方添加的
        ];
        $list = $this->model
            ->field('*')
            ->where($where)
            ->order($sort, $order)
            ->select();
        $data = [
            'list' => $list
        ];
        $this->success('列表', '', $data);
    }
}